import json
import os.path


import openpyxl
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet
from common.dolog import DoLog
my_path='D:\work\ecshop\datas'
dl = DoLog()
myLogger=dl.getLogger(__name__)
class DoFile():
    # 读取文件
    # def doExcelRead(self,fname):
    #     fileName = os.path.join(my_path,fname)
    #     try:
    #         wk:Workbook=openpyxl.load_workbook(fileName)   # 获取工作簿的对象
    #         st:Worksheet = wk["ac"] # 通过工作簿对象获取sheet对象
    #         print(st,type(st))
    #         ss = st.iter_rows(values_only=True) # 获取sheet页中的值
    #         print(ss)
    #         print(range(10))
    #     except Exception as e:
    #         print(f'发生异常，异常是{e}')
    #         myLogger.info(e)
    #     else:
    #         l=[i for i in ss]   # 生成列表并返回
    #         print(f'{l}')
    #         myLogger.info('正常执行无异常')
    #         return l

        def doExcelRead(self, fname, stname, minRow=None, maxRow=None, minCol=None, maxCol=None):
            fileName = os.path.join(my_path, fname)
            try:
                myLogger.info("开始运行")
                wk: Workbook = openpyxl.load_workbook(fileName)
                st: Worksheet = wk[stname]
                print(st, type(st))
                ss = st.iter_rows(min_row=minRow, max_row=maxRow, min_col=minCol, max_col=maxCol, values_only=True)
                # for i in ss:
                #     print(i)
                print("开始运行")
            except Exception as e:
                print(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
                myLogger.exception(f"{fileName}文件,sheet名{stname}处理发生了异常，异常信息是{e}")
            else:
                print(f"处理文件{fileName},{stname}未发生异常")
                myLogger.info(f"处理文件{fileName},{stname}未发生异常")
                l = [i for i in ss]
                print(f"{l}")
            finally:
                print(f"{fileName}处理结束了")
                myLogger.info(f"{fileName}处理结束了")
                wk.close()
                return l

if __name__ == '__main__':
    df = DoFile()
    df.doExcelRead('apt.xlsx', 'Sheet1', 2, 4, 4, 6)

















